package org.compiere.report;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.compiere.model.MAttachmentEntry;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;

/**
 * @author A.Kulantayev
 *
 */
public class A_Asset_Form_434 extends SvrProcess {
	//Form_434_
		/** Current context */
		private Properties m_ctx;
		private ProcessInfo pi;
		int p_A_Asset_Transaction_ID;
		/** */
		String file;

		/**
		 * Prepare - e.g., get Parameters.
		 */
		@Override
		protected void prepare() {
			log.info("");
			m_ctx = Env.getCtx();
			pi = getProcessInfo();
			ProcessInfoParameter[] para = getParameter();
			for (int i = 0; i < para.length; i++) {
				String name = para[i].getParameterName();
				if (para[i].getParameter() == null)
					;
				else if (name.equals("A_Asset_Transaction_ID")) 
					p_A_Asset_Transaction_ID  = para[i].getParameterAsInt();
				else
					log.log(Level.SEVERE, "Unknown Parameter: " + name);
			}

		}

		@Override
		protected String doIt() throws Exception {
			file = Util.localFilePath + "Form_434_.xls";
			MAttachmentEntry entry = Util.getAttachment(pi, m_ctx, file);
			if (entry == null)
				throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

			HSSFWorkbook wb = new HSSFWorkbook(entry.getInputStream());
			HSSFSheet sheet = wb.getSheetAt(0);
			Row R = null;
			Cell C = null;
			//---------------------------------------------------------------
//			R = sheet.getRow(9);
//			C = R.getCell(0);
//			C.setCellValue(MOrg.get(m_ctx,Env.getAD_Org_ID(m_ctx)).getName());
			
//			R = sheet.getRow(15);
//			C = R.getCell(28);
//			C.setCellValue("Иванов Иван Иванович");
//			
//			R = sheet.getRow(17);
//			C = R.getCell(29);
//			C.setCellValue(new SimpleDateFormat("dd").format(new Timestamp(System.currentTimeMillis())));
//			C = R.getCell(32);
//			C.setCellValue(new SimpleDateFormat("MMMMM").format(new Timestamp(System.currentTimeMillis())));
		
			//---------------------------------------------------------------
			// Write the output to a file
			FileOutputStream fileOut = new FileOutputStream(file);
			wb.write(fileOut);
			fileOut.close();
//			try {
//					//Forces Excel to use the specified file as a template for the default workbook. 
//					Runtime.getRuntime().exec("cmd /c start excel " + file+" /t");
//				} 
//			catch (Exception e) 
//				{
//					e.printStackTrace();
//				}
			
			// Open the file with the application associated
						Util.launchFile(new File(file));
					//
			
			return Msg.translate(m_ctx, "Success");
		}

}
